<?
include 'root.php';
Class Forum extends Root {
	public function __construct() {
		parent::__construct();
	}

	public function index() {
		$this -> checkAuthen("forum", "view");
		$this -> data['header'] = "webboard";
		$this -> pagination -> create_links();
		$this -> load -> model("forummodel");
		$allRow = $this -> forummodel -> getForumCount();
		$pageConfig = $this -> initPagination($allRow);
		;
		$this -> data['pagination'] = $this -> pagination;
		$this -> data['canCreate'] = $this -> canUseFunction("forum", "create");
		$this -> data['dataList'] = $this -> forummodel -> getForum($pageConfig['start'], $pageConfig['limit']);
		$this -> includeView("viewforum");
	}

	public function view($forumID) {
		$this -> data['header'] = "webboard";
		$this -> addJS(array("editor", "editorwy"));
		$this -> load -> model("forummodel");
		$this -> load -> library('genhtml');
		$this -> data['genhtml'] = $this -> genhtml;
		$this -> forummodel -> updateView($forumID);
		//dy อย่าลมแก้บรรทัดนี้
		$this -> data['dataList'] = $this -> forummodel -> getAForumAndDetail($forumID, "");
		$this -> data['header'] = $this -> data['dataList'] -> title;
		$this -> genhtml -> setData($this -> data['dataList']);
		$this -> data['canEdit'] = $this -> canUseFunction("forum", "edit");
		$this -> data['canDelete'] = $this -> canUseFunction("forum", "delete");
		$this -> includeView("showforum");
	}

	public function edit($forumID) {
		$this -> checkAuthen("forum", "edit");
		$this -> addJS(array("editor", "editorwy"));
		$this -> load -> model("forummodel");
		$this -> load -> library('genhtml');

		$this -> data['isAdmin'] = $this -> isAdmin();

		$this -> data['genhtml'] = $this -> genhtml;
		$this -> data['dataList'] = $this -> forummodel -> getAForum($forumID, $this -> checkIDSearch("forum", "edit"));
		$this -> data['header'] = $this -> data['dataList'] -> title;
		$this -> genhtml -> setData($this -> data['dataList']);
		$this -> includeView("editforum");

	}

	public function replyforum() {

		$data = $this -> Post2Array($_POST);
		unset($data['_wysihtml5_mode']);
		$data['title'] = "re: " . $data['title'];
		$this -> load -> model("forummodel");
		$this -> forummodel -> replyForum($data);
		$this -> set_res("true", "อัพเดทข้อมูลเรียบร้อยแล้ว");
	}

	public function deleteForum() {
		$this -> load -> model("forummodel");
		$this -> forummodel -> deleteForum($_POST['id'], $_POST['isMain']);
		$this -> set_res("true", "อัพเดทข้อมูลเรียบร้อยแล้ว");
	}

	public function addData() {
		$this -> load -> model("forummodel");
		$id = $this -> forummodel -> createData();
		redirect($this -> data['current'] . "edit/" . $id);
	}

	public function uploadImage() {
		
		$this -> load -> library('image','image');

		
		$fileSave = time() . rand() . "." . $this->get_file_extension($_FILES["files"]['name'][0]);
		$uploadfile = "file_upload/";

		$width = 800;
		$height = 600;
		$this->image-> fromUpload($_FILES['files']);
		$this->image-> resizeImage($width, $height, "auto");
		$this->image-> setSaveName( $fileSave);
		$process = $this->image-> saveImage($uploadfile, 100);


		if ($process) {
			$this->set_res("true", $fileSave);
		} else {
			$this->set_res("false", "error on upload");
		}
	}

	function get_file_extension($file_name) {
		return substr(strrchr($file_name, '.'), 1);
	}

	public function updateData() {
		$data = $this -> Post2Array($_POST);
		if ($data['title'] == "" || $data['message'] == "") {
			$this -> set_res("false", "กรุณากรอกข้อมูลให้ครบถ้วน");
		}
		unset($data['_wysihtml5_mode']);
		$data['status'] = 'Active';
		$data['lastPostID'] = $this -> session -> userdata("sale_no");
		$data['lastPostName'] = $this -> session -> userdata("sale_name");
		$data['updateOn'] = date("Y-m-d H:i:s");
		$data['isPin'] = $this -> checkDataCheckbox("isPin", "N");
		$data['forumSeq'] = 3;
		if (isset($data['forumType'])) {
			if ($data['forumType'] == "Hot") {
				$data['forumSeq'] = 1;
			} else if ($data['forumType'] == "New") {
				$data['forumSeq'] = 2;
			} else {
				$data['forumSeq'] = 3;
			}
		}

		$this -> load -> model("forummodel");
		$this -> forummodel -> updateForum($data);

		$this -> set_res("true", "อัพเดทข้อมูลเรียบร้อยแล้ว");
	}

}
?>